"use strict";

/**
 * 
 * @desc 删除字符串首尾空格或特定字符
 * @param  {String} str 
 * @param  {String|Array} chars 
 * @return {String} 
 */
function trim(str, chars) {
  if (typeof str === "number" || typeof str === "undefined" || typeof str === "boolean") {
    return str;
  }

  if (!str) return;
  var string = str.toString();

  if (!chars) {
    return string.replace(/^\s+|\s+$/g, "");
  } else if (typeof chars === "string") {
    var arrStr = string.split("");
    var arr = [];

    for (var i = 0; i < arrStr.length; i++) {
      if (arrStr[i] !== chars) arr.push(arrStr[i]);
    }

    return arr.join("");
  } else if (chars instanceof Array) {
    var arrStr = string.split("");

    for (var i = 0; i < chars.length; i++) {
      for (var j = 0; j < arrStr.length; j++) {
        if (chars[i] === arrStr[j]) arrStr.splice(j, 1);
      }
    }

    return arrStr.join("");
  }

  return string;
}

module.exports = trim;